package com.bto.solar.work.controller;

import com.bto.solar.work.common.R;
import com.bto.solar.work.dao.IUserSearchRecordService;
import com.bto.solar.work.entity.User;
import com.bto.solar.work.entity.UserSearchRecord;
import com.bto.solar.work.service.BtoUserService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 王小波
 * @description:  用户搜索记录
 * @date 2023/10/19 15:25
 */
@CrossOrigin
@RestController
@RequestMapping("/user-search-record")
public class UserSearchRecordController {
    @Autowired
    private IUserSearchRecordService userSearchRecordService;
    @Autowired
    private BtoUserService btoUserService;
    /**
    * 用户搜索记录
    */
    @ApiModelProperty("查询用户最近搜索记录")
    @GetMapping()
    public R getUserSearchRecord(){
        User currentUser = btoUserService.getCurrentUser();
        List<UserSearchRecord> userSearchRecords = userSearchRecordService.lambdaQuery()
                .eq(UserSearchRecord::getUserId, currentUser.getUserId())
                .like(UserSearchRecord::getSearchContent , "queryTag")
                .orderByDesc(UserSearchRecord::getCreateTime)
                .last("LIMIT 20")
                .list();
        return R.ok(userSearchRecords);
    }
}
